package com.dji.ux.sample.Utils;
import android.os.Build;
import android.os.Environment;
import android.util.Log;

import androidx.annotation.RequiresApi;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import jxl.Workbook;
import jxl.read.biff.BiffException;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;

/**
 * 二次定位飞控数据记录工具
 */
public class ExcelDataUtil {
    private static File file;
    private static WritableWorkbook book;
    private static WritableSheet sheet;
    private static WritableSheet lastSheet;
    private static Workbook workbook;
    private static String[] titles = {"time","Dx", "Dy", "speed_x", "speed_y", "speed_yaw", "speed_h", "height", "integral_x", "integral_y", "error_x", "error_y", "focalLength"};
    private static String path = Environment.getExternalStorageDirectory().toString() + File.separator + "DJI_LOG" + File.separator + "flightController.xls";
//    private static String path = "D:\\文档\\c.xls";
    public ExcelDataUtil() {
    }

    /**
     * 新建数据表，在每次开始推流的时候调用
     * @throws WriteException
     * @throws BiffException
     */
    public static void creatNewBook(String sheetName) {
//        SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss");
//        Date date = new Date(System.currentTimeMillis());
//        String sheetName = formatter.format(date);
        System.out.println("BookName: " + path);
        try {
            file = new File(path);
            File fileParent = file.getParentFile();
            if (!fileParent.exists()){
                fileParent.mkdirs();
            }
            if (!file.exists()){
                file.createNewFile();
                book = Workbook.createWorkbook(file);
                sheet = book.createSheet(sheetName, 0);
            }else {
                workbook = Workbook.getWorkbook(file);
                book = Workbook.createWorkbook(file, workbook);
                int lastSheetIndex = book.getNumberOfSheets()-1;
                lastSheet = book.getSheet(lastSheetIndex);
                //检查excel表上一个sheet，若数据少于10条则作为垃圾数据做删除处理
                if (lastSheet.getRows()<10) book.removeSheet(lastSheetIndex);
                sheet = book.createSheet(sheetName, book.getNumberOfSheets());
            }
            for (int i = 0; i < titles.length; i++) {
                Label label = new Label(i, 0, titles[i]);
                sheet.addCell(label);
            }
            book.write();
            book.close();
        }catch (IOException | WriteException | BiffException e) {
            e.printStackTrace();
        }
    }

    /**
     * 把算法服务器返回的dx，dy以及pid算法得出的速度写入数据表
     * @param params
     * @throws WriteException
     * @throws IOException
     * @throws BiffException
     */
    public static void writeData(Float... params) {
        try {
            file = new File(path);
            workbook = Workbook.getWorkbook(file);
            book = Workbook.createWorkbook(file, workbook);
            sheet = book.getSheet(book.getNumberOfSheets()-1);
            int row = sheet.getRows();
//            Log.d("rows: ",String.format("rows: %d", row));
//            Log.d("params: ",para         ms.toString());
            SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date = new Date(System.currentTimeMillis());
            String stime = formatter.format(date);
            System.out.print(stime);
            Label time = new Label(0, row, stime);
            sheet.addCell(time);
            for (int i = 0; i < params.length; i++) {
                jxl.write.Number number = new jxl.write.Number(i+1, row, params[i]);
                sheet.addCell(number);
            }
            book.write();
            book.close();
        }catch (IOException | WriteException | BiffException e) {
            e.printStackTrace();
        }
    }

    /**
     *Test Sample
     * @throws IOException
     * @throws WriteException
     * @throws BiffException
     */
    @RequiresApi(api = Build.VERSION_CODES.N)
    public static void main(String[] args) throws IOException, WriteException, BiffException {
        SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss");
        Date date = new Date(System.currentTimeMillis());
        String sheetName = formatter.format(date);
        creatNewBook(sheetName);
        for (int i=0;i<100;i++){
            writeData(1f, 0.1f, 0.3f, 0.4f, 0.5f, 43f, 23f, 23f, 13f,1f,2f,4f);
        }
    }

}